<template>
  <div class="login-container">
    <div class="login-box">
      <div class="login-header">
        <h2>会议室预订系统</h2>
        <p class="subtitle">欢迎回来，请登录您的账号</p>
      </div>
      
      <form @submit.prevent="handleSubmit" class="login-form">
        <div class="form-group">
          <label for="username">
            <el-icon><User /></el-icon>
            用户名
          </label>
          <el-input
            id="username"
            v-model="formData.username"
            placeholder="请输入用户名"
            :prefix-icon="User"
            clearable
          />
        </div>
        
        <div class="form-group">
          <label for="password">
            <el-icon><Lock /></el-icon>
            密码
          </label>
          <el-input
            id="password"
            v-model="formData.password"
            type="password"
            placeholder="请输入密码"
            :prefix-icon="Lock"
            show-password
          />
        </div>
        
        <div class="form-actions">
          <el-button 
            type="primary" 
            native-type="submit" 
            :loading="loading"
            class="login-button"
          >
            {{ loading ? '登录中...' : '登录' }}
          </el-button>
          <router-link to="/register" class="register-link">
            还没有账号？立即注册
          </router-link>
        </div>
      </form>
    </div>
  </div>
</template>

<script setup>
import { ref } from 'vue'
import { useRouter, useRoute } from 'vue-router'
import { useUserStore } from '@/stores/user'
import { ElMessage } from 'element-plus'
import { User, Lock } from '@element-plus/icons-vue'

const router = useRouter()
const route = useRoute()
const userStore = useUserStore()

const formData = ref({
  username: '',
  password: '',
})

const loading = ref(false)

const handleSubmit = async () => {
  loading.value = true
  try {
    const result = await userStore.loginUser(formData.value)  
    if (result.success) {
      ElMessage.success('登录成功')
      const redirectPath = route.query.redirect || getDefaultRoute()
      router.replace(redirectPath)
    } else {
      // 优先展示后端返回的 data 字段
      console.log(result.data)
      ElMessage.error(result.data || result.message || '登录失败')
    }
  } catch (error) {
    ElMessage.error('登录失败：' + error.message)
  } finally {
    loading.value = false
  }
}

// 根据用户身份获取默认路由
const getDefaultRoute = () => {
  switch (userStore.user.identity) {
    case 'USER':
      return '/client/dashboard'
    case 'EMPLOYEE':
      return '/employee/room-status'
    case 'ADMIN':
      return '/admin/users'
    default:
      return '/'
  }
}
</script>

<style scoped>
.login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  background: linear-gradient(135deg, #b92b27 0%, #1565c0 100% );
  padding: 1rem;
}





.login-box {
  background: rgba(255, 255, 255, 0.95);
  padding: 2.5rem;
  border-radius: 16px;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.1);
  width: 100%;
  max-width: 420px;
  backdrop-filter: blur(10px);
  border: 1px solid rgba(255, 255, 255, 0.2);
}

.login-header {
  text-align: center;
  margin-bottom: 2.5rem;
}

.login-header h2 {
  font-size: 1.8rem;
  color: #2c3e50;
  margin-bottom: 0.5rem;
  font-weight: 600;
}

.subtitle {
  color: #666;
  font-size: 1rem;
  margin: 0;
}

.login-form {
  display: flex;
  flex-direction: column;
  gap: 1.5rem;
}

.form-group {
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
}

.form-group label {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  color: #2c3e50;
  font-weight: 500;
  font-size: 0.95rem;
}

:deep(.el-input__wrapper) {
  box-shadow: 0 2px 6px rgba(0, 0, 0, 0.05);
  border-radius: 8px;
  padding: 0.5rem 1rem;
}

:deep(.el-input__inner) {
  height: 42px;
}

:deep(.el-input__prefix-inner) {
  color: #909399;
}

.form-actions {
  display: flex;
  flex-direction: column;
  gap: 1rem;
  margin-top: 1rem;
}

.login-button {
  width: 100%;
  height: 44px;
  font-size: 1rem;
  font-weight: 500;
  border-radius: 8px;
  background: linear-gradient(135deg, #b92b27 0%, #1565c0 100%);
  border: none;
  transition: transform 0.2s ease;
}

.login-button:hover {
  transform: translateY(-1px);
}

.register-link {
  text-align: center;
  color: #764ba2;
  text-decoration: none;
  font-size: 0.95rem;
  transition: color 0.2s ease;
}

.register-link:hover {
  color: #b92b27;
}

/* 响应式调整 */
@media screen and (max-width: 480px) {
  .login-box {
    padding: 2rem;
  }

  .login-header h2 {
    font-size: 1.5rem;
  }

  .subtitle {
    font-size: 0.9rem;
  }
}
</style> 